package com.fintech.third.task;

import java.util.Date;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.fintech.common.enums.RespStatus;
import com.fintech.common.util.ApiResponse;
import com.fintech.third.constant.ThirdMethodConstants;
import com.fintech.third.dao.MoxieTaskInfoMapper;
import com.fintech.third.model.MoxieTaskInfo;
import com.fintech.third.model.ThirdProperties;
import com.fintech.third.model.moxie.fund.UserFundRecord;
import com.fintech.third.service.moxie.MoXieService;
import com.fintech.third.utils.SerialNumCreater;
import com.fintech.third.utils.ThirdPropertiesInitUtil;
import com.marvels.common.utils.MarvelsLogUtil;

public class MoXieFundTask implements Job {
	private static final ThirdProperties thirdProperties = ThirdPropertiesInitUtil.getThirdProperties();
	@Autowired
	private MoxieTaskInfoMapper moxieTaskInfoMapper;

	@Autowired(required=true)
	private MoXieService freshMoXie;


	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		/*if (!Boolean.parseBoolean(thirdProperties.getIs_mx_new_programme_user())) {// 启用新方法
			return;
		}
		List<MoxieTaskInfo> list = moxieTaskInfoMapper.selectMoxieTaskInfo("2");
		if (list == null || list.size() < 1) {
			MarvelsLogUtil.getInstance().info("================没有需要执行的魔蝎公积金任务");
			return;
		}
		for (MoxieTaskInfo info : list) {
			
			int i=moxieTaskInfoMapper.updateMoxieTaskInfo(info.getId(), "1");//先将状态改成认证中
			if(i<1){
				
				continue;
			}
			
			String loanId = info.getLoanId();
			String taskId = info.getTaskId();
			MarvelsLogUtil.getInstance().info("=============公积金数据采集开始,loanId:" + loanId + ",taskId:" + taskId);
			String serial_num = SerialNumCreater.creater("MX", loanId);
			ApiResponse<UserFundRecord> apiResponse = new ApiResponse<UserFundRecord>();
			while (true) {

				MarvelsLogUtil.getInstance().info("==================公积金查询全部信息开始:" + apiResponse.getMsg() + ",loanId:" + loanId
						+ ",taskId:" + taskId);
				apiResponse = freshMoXie.obtainUserFundRecord(taskId, serial_num, loanId, ThirdMethodConstants.channel);
				if (RespStatus.SUCCESS.getCode() == apiResponse.getCode()) {
					break;
				}
				MarvelsLogUtil.getInstance().info("==================公积金查询全部信息异常:" + apiResponse.getMsg() + ",loanId:" + loanId
						+ ",taskId:" + taskId);

			}

			String result = "";// -3认证失败（请求失败） -2落库失败 -1认证失败（返回数据为空 ） 1认证成功
			if (apiResponse.getCode() == RespStatus.SUCCESS.getCode()) {
				result = "1";
			}

			if (apiResponse.getCode() == RespStatus.NODATA.getCode()) {
				result = "-1";
			}

			if (apiResponse.getCode() == RespStatus.DATABASE_ERROR.getCode()) {
				result = "-2";
			}

			if (apiResponse.getCode() == RespStatus.REQ_ERROR.getCode()) {
				result = "-3";
			}
			if("2".equals(result)){
				//认证成功  插入历史记录表 删除原始表记录
				info.setResult(result);
				info.setUpdatedDate(new Date());
				moxieTaskInfoMapper.insertMoxieTaskInfoHistory(info);
				
				moxieTaskInfoMapper.deleteMoxieTaskInfo(info.getId());
			}else{
				//其他 则修改原始表状态
				moxieTaskInfoMapper.updateMoxieTaskInfo(info.getId(), result);
			}
		}

		MarvelsLogUtil.getInstance().info("==========魔蝎公积金任务完成");
		*/
	}
}
